<template>
  
  <div class="login" @keyup.enter="submitForm('ruleForm')">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="70px" class="login-form">
      <h3 class="title">时间仓商户管理系统</h3>
      <el-form-item label="用户名：" prop="username">
        <el-input type="text" v-model="ruleForm.username" placeholder="请输入姓名" ></el-input>
      </el-form-item>
      <el-form-item label="密  码：" prop="password">
        <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="请输入密码(字母加数字)"></el-input>
      </el-form-item>
      <el-form-item label="验证码：" prop="code">
          <el-input type="text" v-model="ruleForm.code" auto-complete="off" placeholder="请输入验证码：" style="width:150px;float:left;"></el-input>
          <img :src="'data:image/png;base64,'+img" alt="" class="yzm" @click="codeclick">
          <!-- <span class="yzm">123</span> -->
        </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitFormclick('ruleForm')" class="login-btn">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "" || value.length < 6) {
        callback(new Error("请输入正确的密码"));
      } else {
        callback();
      }
    };
    var validatecode = (rule, value, callback) => {
      if (value === "" || value.length < 1) {
        callback(new Error("请输入正确的验证码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
        code:""
      },
      img:'',
      rules: {
        username: [{ validator: validateName, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        code: [{ validator: validatecode, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitFormclick(formName) {
      console.log(111)
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$post({
            api: "/login/do",
            data: {
              userName:this.ruleForm.username,
              passWord:this.ruleForm.password,
              validateCode:this.ruleForm.code
            }
          }).then(({ data }) => {
            console.log(data)
            if(data.code === 0){
             const token = data.data.token;
             let userInfo = data.data;
             
              console.log(token)
              console.log(userInfo)
              localStorage.setItem("timetoken", token);
              localStorage.setItem(
                  "userInfo",
                  JSON.stringify(data.data)
              );
              this.$router.push('/')
            }else{
              this.$message.error(data.msg);
            }
           
            
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getcode(){
      this.$post({
            api: "/login/getVerCode",
            userName: this.ruleForm.username
          }).then(({ data }) => {
            if(data.code === 0){
              this.img = data.data
            }
            // this.img = data.data
           console.log(data)
          });
    },
    codeclick(){
      this.getcode();
    }
  },
  mounted () {
    this.getcode();
    // this.getcode();
  }
};
</script>
<style>
.login-form .el-form-item__label{
  text-align: left;
}
</style>
<style lang="scss" scoped>
.title {
  color: #495060;
  font-size: 22px;
  text-align: center;
  margin-bottom: 26px;
}
.login {
  width: 100%;
  height: 100%;
  background: #495060;
  position: relative;
  &-form {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -200px;
    margin-top: -160px;
    width: 400px;
    height: 320px;
    background: #ccc;
    padding: 20px;
    box-sizing: border-box;
  }
  &-btn {
    width: 100%;
  }
}
.yzm{
  width:135px;
  height:40px;
  background:#fff;
  text-align: center;
  border-radius: 5px;
  float:right;
  /* box-shadow: 2px 2px #fff; */
}
</style>